/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests;

import javax.servlet.http.HttpSession;

/**
 * Instances of this class are used by the user interface when a user requests
 * to log in.
 *
 * @author Angel Sanadinov
 */
public class LoginRequest extends RequestBean
{
    private String username;        //the name of the user
    private String password;        //the user's password
    private HttpSession userSession;//the session associated with the user
    private String userRemoteAddress;//the IP address of the user trying to log in

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public LoginRequest()
    {
        this.username = null;
        this.password = null;
        this.userSession = null;
        this.userRemoteAddress = null;
    }

    /**
     * Constructs the request using the supplied data.
     *
     * @param username the name of the user
     * @param password the user's password
     * @param userSession the session associated with the user
     * @param userRemoteAddress the IP address of the user attempting to log in
     */
    public LoginRequest(String username, String password, HttpSession userSession,
                        String userRemoteAddress)
    {
        this.username = username;
        this.password = password;
        this.userSession = userSession;
        this.userRemoteAddress = userRemoteAddress;
    }

    /**
     * Sets the name of the user.
     *
     * @param username the name of the user
     */
    public void setUsername(String username)
    {
        this.username = username;
    }

    /**
     * Sets the user's password.
     *
     * @param password the user's password.
     */
    public void setPassword(String password)
    {
        this.password = password;
    }

    /**
     * Sets the session associated with the user.
     *
     * @param userSession the user's session
     */
    public void setUserSession(HttpSession userSession)
    {
        this.userSession = userSession;
    }

    /**
     * Sets the IP address of the user attempting to log in.
     *
     * @param userRemoteAddress the user's IP address
     */
    public void setUserRemoteAddress(String userRemoteAddress)
    {
        this.userRemoteAddress = userRemoteAddress;
    }

    /**
     * Retrieves the name of the user.
     *
     * @return the user's name
     */
    public String getUsername()
    {
        return username;
    }

    /**
     * Retrieves the user's password.
     *
     * @return the user's password
     */
    public String getPassword()
    {
        return password;
    }

    /**
     * Retrieves the session associated with the user.
     *
     * @return the user's session
     */
    public HttpSession getUserSession()
    {
        return userSession;
    }

    /**
     * Retrieves the IP address of the user trying to log in.
     *
     * @return the user's IP address
     */
    public String getUserRemoteAddress()
    {
        return userRemoteAddress;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when all data supplied when constructing it is
     * valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(username != null && !username.trim().equals("") && password != null
                && !password.trim().equals("") && userSession != null)
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (username,userRemoteAddress)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return LoginRequest.class.getSimpleName() + ": (" + username + "," + userRemoteAddress + ")";
    }
}
